Crate axum_flash

source ·
Expand description

One-time notifications (aka flash messages) for axum.

Example

use axum::{
    response::{IntoResponse, Redirect},
    extract::FromRef,
    routing::get,
    Router,
};
use axum_flash::{IncomingFlashes, Flash, Key};

#[derive(Clone)]
struct AppState {
    flash_config: axum_flash::Config,
}

let app_state = AppState {
    // The key should probably come from configuration
    flash_config: axum_flash::Config::new(Key::generate()),
};

// Our state type must implement this trait. That is how the config
// is passed to axum-flash in a type safe way.
impl FromRef<AppState> for axum_flash::Config {
    fn from_ref(state: &AppState) -> axum_flash::Config {
        state.flash_config.clone()
    }
}

let app = Router::new()
    .route("/", get(root))
    .route("/set-flash", get(set_flash))
    .with_state(app_state);

async fn root(flashes: IncomingFlashes) -> IncomingFlashes {
    for (level, text) in &flashes {
        // ...
    }

    // The flashes must be returned so the cookie is removed
    flashes
}

async fn set_flash(flash: Flash) -> (Flash, Redirect) {
    (
        // The flash must be returned so the cookie is set
        flash.debug("Hi from flash!"),
        Redirect::to("/"),
    )
}

Structs

  • Configuration for axum-flash.
  • Extractor for setting outgoing flash messages.
  • Extractor for incoming flash messages.
  • An iterator over the flash messages.
  • A cryptographic master key for use with Signed and/or Private jars.

Enums

  • Verbosity level of a flash message.